package com.yfbao.horizon.compress;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import net.jpountz.lz4.LZ4Compressor;
import net.jpountz.lz4.LZ4Factory;
import net.jpountz.lz4.LZ4FastDecompressor;
import org.xerial.snappy.Snappy;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class LZ4Utils {

    private static ObjectMapper objectMapper = new ObjectMapper();

    public static void main(String[] args) {

        for (int i = 0; i < 1; i++) {
            List<CtpEntityResource> list = createTestData(1000);
            compressData(list,1);
        }
    }

    public static long compressData(List<CtpEntityResource> list,int caseTime) {

        try {
            byte[] srcbytes = objectMapper.writeValueAsBytes(list);
//            System.out.println("src size:" + srcbytes.length);
            LZ4Factory factory = LZ4Factory.fastestInstance(); // 使用最快的压缩器
            LZ4Compressor compressor = factory.fastCompressor();
            byte[] srccompress = compressor.compress(srcbytes);
//            System.out.println("zipsrc size:" + srccompress.length);

            long l = System.currentTimeMillis();
            List<CtpEntityResource> listtar = new ArrayList<>();
            for (int j = 0; j < caseTime; j++) {
//                byte[] bytes = objectMapper.writeValueAsBytes(list);
//                byte[] compress = compressor.compress(bytes);
//                System.out.println("zipsrc size:"+compress.length);

                LZ4FastDecompressor lz4FastDecompressor = factory.fastDecompressor();
                byte[] decompress = lz4FastDecompressor.decompress(srccompress, srcbytes.length);
//                List<CtpEntityResource> list1 = objectMapper.readValue(decompress, new TypeReference<List<CtpEntityResource>>() {});

//                System.out.println(objectMapper.writeValueAsString(list1.get(0)));
//                System.out.println(list1.get(0).getEntityId());
            }
            long cost =(System.currentTimeMillis() - l);
            System.out.println("cost:" + cost);
            return cost;
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return -1;

    }

    public static List<CtpEntityResource> createTestData(int listSize) {
        List<CtpEntityResource> list = new ArrayList<>();
        for (int i = 0; i < listSize; i++) {
            CtpEntityResource entityResource = new CtpEntityResource();
            entityResource.setCreatorId(1231231231L);
            entityResource.setAction(2);
            entityResource.setEntityId(11231231231251251L);
            entityResource.setEntityType("type");
            entityResource.setExtend("extend");
            entityResource.setIncludeChild(false);
            entityResource.setOrgId(12312312312L);
            entityResource.setOriginId(12111111L);
            entityResource.setResourceAdmin(false);
            entityResource.setResourceAllowEnd(false);
            entityResource.setResourceAppId("ctp-data");
            entityResource.setResourceClientType("pc");
            entityResource.setResourceCode("useer---aaa");
            entityResource.setResourceDataType("datatype");
            entityResource.setResourceEntityFullName("nihao");
            entityResource.setResourceIcon("#FAAAAEADA");
            entityResource.setResourceId(123123123123L);
            entityResource.setResourceLevel(2);
            entityResource.setResourceName("edoc12312312312312312");
            entityResource.setResourceOpenType("open");
            entityResource.setResourceOrgId(1123123123123L);
            entityResource.setResourceParentId(1111111111111L);
            entityResource.setResourcePath("/path/user/edoc1123123123131231/html");
            entityResource.setResourcePermissionType("permType");
            entityResource.setResourceSortNumber(2);
            entityResource.setResourceType("RESOURCETYPE");
            entityResource.setResourceUrl("");
            entityResource.setRoleId(11112312312312L);
            list.add(entityResource);
        }
        return list;
    }

    public static String getCompressRatio(List<CtpEntityResource> list)  {
        try {
            byte[] srcbytes = objectMapper.writeValueAsBytes(list);

            LZ4Factory factory = LZ4Factory.fastestInstance(); // 使用最快的压缩器
            LZ4Compressor compressor = factory.fastCompressor();
            byte[] srccompress = compressor.compress(srcbytes);
//            System.out.println("src size:" + srcbytes.length);
//            System.out.println("zipsrc size:" + srccompress.length);
            Double percentage = ((double)srccompress.length)/((double)srcbytes.length)*100;
            String s = String.valueOf(percentage);
            int i = s.indexOf(".");
            return s.substring(0,i+5);
        }catch (Exception e){
            System.out.println(e);
        }
      return null;
    }

}
